<template>
  <div id="teamUpdatePage">
    <van-form @submit="onSubmit">
      <van-cell-group inset>
        <van-field
            v-model="updateTeamData.name"
            name="name"
            label="队伍名"
            placeholder="请输入队伍名"
            :rules="[{ required: true, message: '请输入队伍名' }]"
        />
        <van-field
            v-model="updateTeamData.description"
            rows="4"
            autosize
            label="队伍描述"
            type="textarea"
            placeholder="请输入队伍描述"
        />
        <van-field
            is-link
            readonly
            name="datetimePicker"
            label="过期时间"
            :placeholder="updateTeamData.expireTimeVO && updateTeamData.expireTimeVO[0] !== '' && updateTeamData.expireTimeVO[1] !== '' && updateTeamData.expireTimeVO[2] !== '' ? `${updateTeamData.expireTimeVO[0]}年${updateTeamData.expireTimeVO[1]}月${updateTeamData.expireTimeVO[2]}日`: '点击选择过期时间'"
            @click="showPicker = true"
        />
        <van-popup v-model:show="showPicker" position="bottom">
          <van-date-picker
              v-model="updateTeamData.expireTimeVO"
              @confirm="showPicker = false"
              type="datetime"
              title="请选择过期时间"
              :formatter="formatter"
              :min-date="minDate"
          />
        </van-popup>
        <van-field name="radio" label="队伍状态">
          <template #input>
            <van-radio-group v-model="updateTeamData.status" direction="horizontal">
              <van-radio name="0">公开</van-radio>
              <van-radio name="1">私有</van-radio>
              <van-radio name="2">加密</van-radio>
            </van-radio-group>
          </template>
        </van-field>
        <van-field
            v-if="Number(updateTeamData.status) === 2"
            v-model="updateTeamData.password"
            type="password"
            name="password"
            label="密码"
            placeholder="请输入队伍密码"
            :rules="[{ required: true, message: '请填写密码' }]"
        />
      </van-cell-group>
      <div style="margin: 16px;">
        <van-button round block type="primary" native-type="submit">
          提交
        </van-button>
      </div>
    </van-form>
  </div>
</template>

<script setup lang="ts">

import {useRoute, useRouter} from "vue-router";
import {onMounted, ref} from "vue";
import myAxios from "../plugins/myAxios.ts";
import {showFailToast, showSuccessToast} from "vant";
import {formatDateArrayToISOString} from "../services/formatDateArrayToISOString.ts";

const router = useRouter();
const route = useRoute();
const updateTeamData = ref({
  "name": "",
  "description": "",
  "expireTime": "",
  "expireTimeVO": ['', '', ''],
  "password": "",
  "status": 0,
  "userId": 0
});
const id = Number(route.query.id);
// 展示日期选择器
const showPicker = ref(false);
const minDate = new Date(2022, 2, 2);

onMounted(async () => {
  if (id <= 0) {
    showFailToast("队伍加载失败，请重试");
    return;
  }
  const res = await myAxios.get('/team/get', {
    params: {
      id,
    }
  })
  if (res?.code === 0) {
    updateTeamData.value = res.data;
    updateTeamData.value.expireTimeVO = res.data.expireTime.split('T')[0].split('-');
  } else {
    showFailToast("加载队伍失败，请重试")
  }

})

const formatter = (type, option) => {
  if (type === 'year') {
    option.text += '年';
  }
  if (type === 'month') {
    option.text += '月';
  }
  if (type === 'day') {
    option.text += '日';
  }
  return option;
};

//提交
const onSubmit = async () => {
  updateTeamData.value.expireTime = formatDateArrayToISOString(updateTeamData.value.expireTimeVO)
  const postData = {
    ...updateTeamData.value,
    status: Number(updateTeamData.value.status)
  }
  //todo 前端数据校验
  const res = await myAxios.post("/team/update", postData);
  if (res?.code === 0 && res.data) {
    showSuccessToast("添加成功");
    router.push({
      path: '/team',
      replace: true,
    });
  } else {
    showFailToast("添加失败")
  }
}


</script>

<style scoped>

</style>